package arrAndstr;

import java.util.ArrayList;
import java.util.List;

/*
    228. 汇总区间
 */
public class T228 {

    public static void main(String[] args) {
        int[] nums = new int[]{0, 2, 3, 4, 6, 8, 9};
        System.out.println(summaryRanges(nums));
    }

    public static List<String> summaryRanges(int[] nums) {
        int len = nums.length;
        List<String> result = new ArrayList<>();
        if (len == 0) return result;
        if (len == 1) {
            result.add(nums[0] + "");
            return result;
        }
        int leftIndex = 0;
        int rightIndex = leftIndex + 1;
        while (true) {
            if (rightIndex == nums.length) {
                if (rightIndex - leftIndex == 1) {
                    result.add(nums[leftIndex] + "");
                } else {
                    result.add(nums[leftIndex] + "->" + nums[rightIndex - 1]);
                }
                break;
            }
            if (nums[rightIndex] - nums[rightIndex - 1] == 1) {
                rightIndex++;
                continue;
            }
            if (rightIndex - leftIndex == 1) {
                result.add(nums[leftIndex] + "");
            } else {
                result.add(nums[leftIndex] + "->" + nums[rightIndex - 1]);
            }
            leftIndex = rightIndex;
            rightIndex = leftIndex + 1;
        }
        return result;
    }
}
